package com.blog.controller.adminarticle;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.blog.controller.base.BaseServlet;
import com.blog.entity.AdminArticleEntity;
import com.blog.entity.AdminArticleQueryEntity;
import com.blog.service.frontpage.AdminArticleService;
import com.blog.utils.JSONUtil;
import com.blog.utils.PageBean;

import net.sf.json.JSONObject;

@WebServlet("/adminarticletime")
public class AdminArticleTimeServlet extends BaseServlet {

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	
		String startTime=req.getParameter("startTime");
		String endTime=req.getParameter("endTime");
		String key=req.getParameter("key");
		int pageNum=req.getParameter("pageNum")==null?1:Integer.parseInt(req.getParameter("pageNum"));
		int pageSize=req.getParameter("pageSize")==null?3:Integer.parseInt(req.getParameter("pageSize"));
		AdminArticleQueryEntity e=new AdminArticleQueryEntity();
		if(startTime!=null&&!startTime.equals("")) {
			e.setStarttime(Date.valueOf(startTime));
		}
		if(endTime!=null&&!endTime.equals("")) {
			e.setEndTime(Date.valueOf(endTime));
		}
		if(key!=null&&!key.equals("")) {
			e.setKey(key);
		}
		e.setPageSize(pageSize);
		e.setStartindex(pageSize*(pageNum-1));
		e.setStatus(0);
	
		AdminArticleService service=AdminArticleService.getInstance();
		List<AdminArticleEntity> articles=service.getNewAdminArticle(e);
		int totalRecord=(int) service.getTotalRecord(e).getTotalRecord();
		PageBean<AdminArticleEntity> bean=new PageBean<AdminArticleEntity>(pageNum,pageSize,totalRecord);
		bean.setList(articles);
		JSONObject object=JSONUtil.BeanToJson(bean);
		PrintWriter out=resp.getWriter();
		out.print(object.toString());
		System.out.println("sdffsdfdsfdssdfsdf");
		/*String row="";
		PrintWriter out=resp.getWriter();
		for(AdminArticleEntity a:articles) {
			row+="<tr blogid="+a.getArticle_id()+">\r\n" + 
					"								<td><input type=\"checkbox\"></td>\r\n" + 
					"								<td>"+a.getArticle_title()+"</td>\r\n" + 
					"								<td>"+a.getTypes_name()+"</td>\r\n" + 
					"								<td>"+a.getUser_name()+"</td>\r\n" + 
					"								<td>"+a.getPublish_time()+"</td>\r\n" + 
					"								<td>"+a.getArticle_supportnumber()+"</td>\r\n" + 
					"								<td>"+a.getArticle_readnumber()+"</td>\r\n" + 
					"								<td>"+a.getArticle_discussnumber()+"</td>\r\n" + 
					"								<td><input type=\"button\" value=\"删除\" class=\"inputtype\"></td>\r\n" + 
					"							</tr>";
		
		}
		out.println(row);*/
		
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
}